package com.cdbkj.cloud.pos.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.text.method.ScrollingMovementMethod;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cdbkj.cloud.pos.KeyboardActivity;
import com.cdbkj.cloud.pos.R;

/**
 * Created by luozongsheng on 22/06/2018.
 */

public class CustomPwdDialog extends Dialog {

    protected View mView;

    //ui
    protected TextView mTextTitle,mTextCancel,mTextSure;
    protected TextView editText;

    public CustomPwdDialog(Context context) {
        this(context,0);
    }

    public CustomPwdDialog(Context context, int themeResId) {
        super(context, themeResId);
        mView = LayoutInflater.from(context).inflate(R.layout.view_custom_pwd_dialog,null);
        setContentView(mView);
        initView(context);
        //setting size
        Window dialogWindow = this.getWindow();
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        //手机横竖屏时候
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的
            p.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的
        }else if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的
            p.width = (int) (d.getWidth() * 0.5); // 宽度设置为屏幕的
        }
        dialogWindow.setAttributes(p);
    }

    private void initView(Context context) {
        try{
            mTextTitle = (TextView) mView.findViewById(R.id.view_custom_alter_dialog_title);
            mTextCancel = (TextView) mView.findViewById(R.id.view_custom_alter_dialog_cancel);
            mTextSure = (TextView) mView.findViewById(R.id.view_custom_alter_dialog_sure);

            editText = (TextView)findViewById(R.id.editText1);
            editText.setEnabled(false);

            TextView[] mBtnkey_digits = new TextView[10];
            for (int i = 0; i < 10; i++) {
                String strid = String.format("btn_price_%d", i);
                mBtnkey_digits[i] = (TextView)findViewById(context
                        .getResources().getIdentifier(strid, "id",
                                context.getPackageName()));
                mBtnkey_digits[i].setOnClickListener(mClickListener);
            }
            TextView del_tv = (TextView) findViewById(R.id.btn_num_del);

            del_tv.setOnClickListener(mClickListener);

        }catch(Exception _e){

        }
    }


    public void setText(String _title,String _cancel,String _sure){
        try{
            if (null != _title)
                mTextTitle.setText(_title);
            if (null != _cancel)
                mTextCancel.setText(_cancel);
            if (null != _sure)
                mTextSure.setText(_sure);
        }catch(Exception _e){

        }
    }

    public void setLeftOnclick(View.OnClickListener _onclick){
        try{
            mTextCancel.setOnClickListener(_onclick);
        }catch(Exception _e){

        }
    }

    public void setRightOnclick(View.OnClickListener _onclick){
        try{
            mTextSure.setOnClickListener(_onclick);
        }catch(Exception _e){

        }
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.btn_price_1
                    || id == R.id.btn_price_2
                    || id == R.id.btn_price_3
                    || id == R.id.btn_price_4
                    || id == R.id.btn_price_5
                    || id == R.id.btn_price_6
                    || id == R.id.btn_price_7
                    || id == R.id.btn_price_8
                    || id == R.id.btn_price_9
                    || id == R.id.btn_price_0) {
                String input = ((TextView) view).getText().toString();
                if (input == null){
                    editText.setText(input);
                }else if (input != null) {
                    String strTmp = editText.getText().toString();
                    if(strTmp.length()>=6){
                        return;
                    }
                    strTmp += input;
                    editText.setText(strTmp);
                }
                editText.setTextSize(30);
                editText.setTextColor(Color.BLACK);
            }else if (id == R.id.btn_num_del) {//清除
                if (editText.getText().length() > 0) {
                    String strTmp = editText.getText().toString();
                    strTmp = strTmp.substring(0, strTmp.length() - 1);
                    editText.setText(strTmp);
                }else {
                    editText.setText("");
                }
                editText.setTextSize(30);
                editText.setTextColor(Color.BLACK);
            }

        }
    };

    public String getPwd(){
        if(editText.getText()!=null){
            return editText.getText().toString();
        }
        return "";
    }

}
